﻿using IMeiWeiWebService.Models;
using IMeiWeiWebService.Providers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace IMeiWeiWebService.Managers
{
    public class BusinessInfoManager
    {
        AreaProvider _areaProvider = new AreaProvider();
        IMeiWei.BLL.business bll_Business = new IMeiWei.BLL.business();
        IdFactoryProvider _idFactoryProvider = new IdFactoryProvider();
        BusinessClassProvider _businessClassProvider = new BusinessClassProvider();
        SenceClassificationProvider _senceClassificationProvider = new SenceClassificationProvider();
        BusinessSelectItemProvider _businessSelectItemProvider = new BusinessSelectItemProvider();
        BusinessSenderTypeProvider _businessSenderTypeProvider = new BusinessSenderTypeProvider();
        IMeiWei.BLL.businessimgs bll_imgs = new IMeiWei.BLL.businessimgs();

        public BusinessInfoModel GetBusinessInfoModel(string businessTypeId, string businessId)
        {
            var result = new BusinessInfoModel();

            var businessRecord = bll_Business.GetModelList(" BusinessId='" + businessId + "' ").FirstOrDefault();

            if (null == businessRecord)
            {
                businessRecord = new IMeiWei.Model.business();
                businessRecord.BusinessId = _idFactoryProvider.GetNewBusinessId(businessTypeId);
            }

            var areaOnes = _areaProvider.LoadAreaOneLeval();
            var areaTwoes = _areaProvider.LoadAreaTwoLeval();
            var businessClassOnes = _businessClassProvider.LoadClassOneItems(businessTypeId);
            var businessClassTwoes = _businessClassProvider.LoadClassTwoItems();
            var senceClassificatioins = _senceClassificationProvider.LoadSenceClassificationes();
            var selectItems = _businessSelectItemProvider.LoadBusinessSelectItem(businessTypeId);
            var selectDetails = _businessSelectItemProvider.LoadBusinessSelectDetail(businessId);
            var imgs = bll_imgs.GetModelList("Business_Id ='" + businessId + "'");
            var businessSenderTypes = _businessSenderTypeProvider.LoadBusinessSenderType();

            result.BusinessId = businessRecord.BusinessId;
            result.BusinessAddress = businessRecord.BusinessAddress;
            result.BusinessHours = businessRecord.BusinessHours;
            result.BusinessName = businessRecord.BusinessName;
            result.BusinessPhone = businessRecord.BusinessPhone;
            result.FirstOffer = businessRecord.FirstOffer;
            result.VIPOffer = businessRecord.VIPOffer;
            result.IsAllelic = businessRecord.IsServerAllelic;
            result.IsPark = businessRecord.IsServerPark;
            result.RestauranThighLights = businessRecord.RestaurantHighlights;
            result.SenderTypeId = businessRecord.BusinessSenderTypeId;
            result.SenderContent = businessRecord.BusinessSenderContent;
            result.PercapitaConsumption = null == businessRecord.PerCapitaConsumption ?
                string.Empty : businessRecord.PerCapitaConsumption.ToString();

            #region Picture hander

            var img1 = imgs.FirstOrDefault(o => o.OrderNo == 1);
            var img2 = imgs.FirstOrDefault(o => o.OrderNo == 2);
            var img3 = imgs.FirstOrDefault(o => o.OrderNo == 3);
            var img4 = imgs.FirstOrDefault(o => o.OrderNo == 4);
            var img5 = imgs.FirstOrDefault(o => o.OrderNo == 5);
            if (null != img1) { result.PicturePath1 = img1.picURL; }
            if (null != img2) { result.PicturePath2 = img2.picURL; }
            if (null != img3) { result.PicturePath3 = img3.picURL; }
            if (null != img4) { result.PicturePath4 = img4.picURL; }
            if (null != img5) { result.PicturePath5 = img5.picURL; }

            #endregion


            #region 一级场景，二级场景

            var selectItemSceneOne = selectItems.Where(o => o.SelectItemName == "环境氛围").FirstOrDefault();
            if (null != selectItemSceneOne)
            {
                var SceneOneItem = selectDetails.Where(o => o.Item_Id == selectItemSceneOne.Id && o.Business_Id == businessId).FirstOrDefault();
                result.SceneOneId = null == SceneOneItem ? string.Empty : SceneOneItem.SelectItem_Id;
            }
            var selectItemSceneTwo = selectItems.Where(o => o.SelectItemName == "环境氛围").FirstOrDefault();
            if (null != selectItemSceneOne)
            {
                var SceneTwoItem = selectDetails.Where(o => o.Item_Id == selectItemSceneOne.Id && o.Business_Id == businessId).FirstOrDefault();
                result.SceneTwoId = null == SceneTwoItem ? string.Empty : SceneTwoItem.SelectItem_Id;
            }
            if (null != senceClassificatioins)
            {
                foreach (var item in senceClassificatioins)
                {
                    result.SceneOnes.Add(new KeyValuePair<string, string>(item.Id, item.SceneName));
                }
                foreach (var item in senceClassificatioins)
                {
                    result.SceneTwoes.Add(new KeyValuePair<string, string>(item.Id, item.SceneName));
                }
            }

            #endregion

            #region 一级菜系，二级菜系

            string classOne = string.Empty;
            string classTwo = string.Empty;

            if (businessTypeId == "D385CF8F-B6E6-437C-81D6-33E0B6EF33DD")
            {
                classOne = "主营菜系";
                classTwo = "选择菜系";
            }
            else
            {
                classOne = "主营项目";
                classTwo = "选择类别";
            }


            var selectItemCuisine = selectItems.Where(o => o.SelectItemName == classTwo).FirstOrDefault();
            if (null != selectItemCuisine)
            {
                var AppCuisineItem = selectDetails.Where(o => o.Item_Id == selectItemCuisine.Id && o.Business_Id == businessId).FirstOrDefault();
                result.AppCuisineId = null == AppCuisineItem ? string.Empty : AppCuisineItem.SelectItem_Id;
            }
            var selectItemDishType = selectItems.Where(o => o.SelectItemName == classOne).FirstOrDefault();
            if (null != selectItemDishType)
            {
                var AppDishTypeItem = selectDetails.Where(o => o.Item_Id == selectItemDishType.Id && o.Business_Id == businessId).FirstOrDefault();
                result.AppDishTypeId = null == AppDishTypeItem ? string.Empty : AppDishTypeItem.SelectItem_Id;
            }
            if (null != businessClassOnes)
            {
                foreach (var item in businessClassOnes)
                {
                    result.AppDishTypes.Add(new KeyValuePair<string, string>(item.Id, item.ClassOneItem));

                    if (null != businessClassTwoes)
                    {
                        var collection = businessClassTwoes.FindAll(o => o.BusinessTypeClassOne_Id == item.Id);
                        var itemTwoLevelList = new TwoLevelClass();
                        itemTwoLevelList.Id = item.Id;
                        if (null != collection)
                        {
                            foreach (var item1 in collection)
                            {
                                itemTwoLevelList.KeyValues.Add(new KeyValuePair<string, string>(item1.Id, item1.BusinessTypeClassTwoName));
                            }
                        }
                        result.AppCuisines.Add(itemTwoLevelList);
                    }
                }
            }


            #endregion

            #region 一级市区，二级区县

            var selectItemAreaOne = selectItems.Where(o => o.SelectItemName == "选择城市").FirstOrDefault();
            if (null != selectItemAreaOne)
            {
                var areaOneItem = selectDetails.Where(o => o.Item_Id == selectItemAreaOne.Id && o.Business_Id == businessId).FirstOrDefault();
                result.AreaOneId = null == areaOneItem ? string.Empty : areaOneItem.SelectItem_Id;
            }
            var selectItemAreaTwo = selectItems.Where(o => o.SelectItemName == "选择地区").FirstOrDefault();
            if (null != selectItemAreaTwo)
            {
                var areaOneItem = selectDetails.Where(o => o.Item_Id == selectItemAreaTwo.Id && o.Business_Id == businessId).FirstOrDefault();
                result.AreaTwoId = null == areaOneItem ? string.Empty : areaOneItem.SelectItem_Id;
            }
            if (null != areaOnes)
            {
                foreach (var item in areaOnes)
                {
                    result.AreaOnes.Add(new KeyValuePair<string, string>(item.Id, item.AreaOne));
                    if (null != areaTwoes)
                    {
                        var collection = areaTwoes.FindAll(o => o.AreaOne_Id == item.Id);
                        var itemTwoLevelList = new TwoLevelClass();
                        itemTwoLevelList.Id = item.Id;
                        if (null!= collection)
                        {
                            foreach (var item1 in collection)
                            {
                                itemTwoLevelList.KeyValues.Add(new KeyValuePair<string, string>(item1.Id, item1.AreaTwo));
                            }
                        }
                        result.AreaTwoes.Add(itemTwoLevelList);
                    }
                }
            }
            

            #endregion

            #region BusinessSenderTypes

            if (null != businessSenderTypes)
            {
                foreach (var item in businessSenderTypes)
                {
                    result.BusinessSenderTypes.Add(new KeyValuePair<string, string>(item.Id, item.SenderType));
                }
            }

            #endregion

            return result;
        }



    }
}